package com.ruicar.afs.cloud.product.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.product.entity.LprConfiguration;
import com.ruicar.afs.cloud.product.entity.ProductPlanRate;
import com.ruicar.afs.cloud.product.enums.StatusEnum;
import com.ruicar.afs.cloud.product.mapper.LprConfigurationMapper;
import com.ruicar.afs.cloud.product.service.LprConfigurationService;
import org.springframework.stereotype.Service;

/**
 * @author xudongdong
 * @date 2020/04/23
 */
@Service
public class LprConfigurationServiceImpl extends ServiceImpl<LprConfigurationMapper, LprConfiguration> implements LprConfigurationService {

    /**
     * @param productRate 利率表信息
     * @return
     * @Description 获取最终的LPR客户利率
     */
    @Override
    public Double getLprNew(Integer loanPeriodStart,Integer loanPeriodEnd) {
        //查询期数再中间的，最新发布日期的LPR利率
        LprConfiguration lprConfiguration = this.getOne(Wrappers.<LprConfiguration>query().lambda()
                        .eq(LprConfiguration::getStatus, AfsEnumUtil.key(StatusEnum.YES))
                        .le(LprConfiguration::getApplicationPeriodStart, loanPeriodStart)
                        .ge(LprConfiguration::getApplicationPeriodEnd, loanPeriodEnd)
                        .orderByDesc(LprConfiguration::getLprDate)
                , false
        );
        //得到查询的结果
        Double lprRate = 0.00;
        if (lprConfiguration != null && lprConfiguration.getLprRate() != null) {
            lprRate = lprConfiguration.getLprRate().doubleValue();
        }
        return lprRate;
    }

    /**
     * @Description 最新的LPR实时生效
     */
    @Override
    public void timingTriggerLPR() {

        this.update(Wrappers.<LprConfiguration>lambdaUpdate()
                .eq(LprConfiguration::getStatus, AfsEnumUtil.key(StatusEnum.NO))//状态=未生效
                .le(LprConfiguration::getSystemEffectiveTime, DateUtil.date())//生效时间小于等于当前时间
                .set(LprConfiguration::getStatus, AfsEnumUtil.key(StatusEnum.YES))//状态=生效
        );

    }


}
